<%@ page import="bean.Product" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="utils.CookieUtils" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/9/22/022
  Time: 18:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品列表</title>
    <script src="../lib/js/jquery3.6.0.js" type="text/javascript"></script>
    <script src="../lib/js/bootstrap.js" type="text/javascript"></script>
    <link rel="stylesheet" href="../lib/css/bootstrap.css">
    <style type="text/css">
        .aaa {
            font-size: 40px;
            color: #007FFF;
            width: 500px;
            height: 200px;
            position: relative;
            left: 300px;

        }
    </style>
</head>
<body>


<h1><a href="../productCart.do">购物车</a></h1>
<%
    Object attribute = session.getAttribute("username");
    if (attribute != null) {
        out.write("<h1><a href=\"#\">欢迎" + attribute + "来到书城");
    } else {
        out.write("<h1><a href=\"../login.jsp\">");
        out.write("请登录");
    }
    out.write("</a></h1>");
%>
<table class="table table-striped">
    <thead>
    <tr>
        <th>商品ID</th>
        <th>商品名称</th>
        <th>商品价格</th>
        <th>商品分类</th>
        <th>商品数量</th>
        <th>商品图片</th>
        <th>商品描述</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <%!
        public static Integer id;
    %>
    <%

        ArrayList<Product> products = (ArrayList<Product>) request.getSession().getAttribute("products");
        if (products == null) {
            response.sendRedirect(request.getContextPath() + "/c.do");
            return;
        }
        for (Product p : products) {
            out.write("<tr>");
            out.write("<td>" + p.getId() + "</td>");
            out.write("<td>" + p.getName() + "</td>");
            out.write("<td>" + p.getPrice() + "</td>");
            out.write("<td>" + p.getCategory() + "</td>");
            out.write("<td>" + p.getPnum() + "</td>");
            out.write("<td><img style='width: 50px;' src=\"" + p.getImgurl() + "\"alt=\" " + "alt" + "\" ></td>");
            out.write("<td>" + p.getDescription() + "</td>");
            out.write("<td><form action=\"../productCart.do\">\n" +
                    "    <input type=\"hidden\" name=\"action\" value=\"addProduct\">\n" +
                    "    <input type=\"hidden\" name=\"pid\" value=\"" + p.getId() + "\">\n" +
                    "    <button class=\"btn-danger\" type=\"submit\">加入购物车</button>\n" +
                    "</form></td>");
            out.write("</tr>");
        }
    %>
    </tbody>

</table>
<div class="aaa">
    <%

        Integer pgn = (Integer) session.getAttribute("pgn");
        Integer tpg = (Integer) session.getAttribute("tpg");

        if (tpg == null) {
            response.sendRedirect(request.getContextPath() + "/c.do?pageNo=" + 1);
            return;
        }
    %>
    <%if (pgn > 1) {%>
    <a href="<%=request.getContextPath() + "/c.do?pageNo=" + (pgn-1)%>"><%="上一页"%>
    </a>
    <%}%>
    <%


        if (pgn == null) {
            pgn = 1;
        }

        int begin = 0;
        Integer[] label = null;
        if (tpg < 5) {
            label = new Integer[tpg];
            for (int i = 0; i < label.length; i++) {
                Integer obj = label[i];
                if (pgn.equals(obj)) {
                    out.write("[<a href=\"" + request.getContextPath() + "\"/c.do?pageNo=" + obj + ">" + obj + "</a>]");
                } else {
                    out.write("<a href=\"" + request.getContextPath() + "\"/c.do?pageNo=" + obj + ">" + obj + "</a>");
                }
            }
            return;
        }
        label = new Integer[5];
        if (pgn < 4) {
            begin = 1;
        } else if (pgn <= tpg - 2) {
            begin = pgn - 2;
        } else if (pgn < tpg + 1) {
            begin = tpg - 4;
        } else if (pgn > tpg) {
            response.sendRedirect(request.getContextPath() + "/c.do?pageNo=" + 1);
            return;
        } else {
            begin = 1;
        }
        for (int i = begin; i < label.length + begin; i++) {
            label[i - begin] = i;
        }
        for (int i = 0; i < 5; i++) {
            Integer obj = label[i];
            if (pgn.equals(obj)) {%>

    [<%=obj%>]
    <%
    } else {%>
    <a href="<%=request.getContextPath() + "/c.do?pageNo=" + obj%>"><%=obj%>
    </a>

    <%
            }
        }
    %>
    <%if (pgn < tpg) {%>
    <a href="<%=request.getContextPath() + "/c.do?pageNo=" + (pgn+1)%>"><%="下一页"%>
    </a>
    <%}%>
</div>
<form action="<%=request.getContextPath() + "/c.do"%>">
    <input type="text" name="pageNo">
    <button type="submit">搜索</button>
</form>

<div class="aaa">
    <%  int length;
        int center = (int) session.getAttribute("pgn");
        int max = (int) session.getAttribute("tpg");

        if (max < 5) {
            begin=1;
            length = max;
        } else{
            length=5;
            if (center - 2 > 0 && center + 2 <= max) {
                begin = center - 2;
            } else if (center - 2 <= 0) {
                begin = 1;
            } else if (center + 2 > max) {
                begin = max - 5;
            }
        }
        for (int i = begin; i < length; i++) {
            if (i==center){%>
    [<%=i%>]
    <%}else {%>
    <a href="<%=request.getContextPath() + "/c.do?pageNo=" + i%>"><%=i%></a>
    <%}
    }

    %>
</div>
</body>
</html>
